package ATM_mysql;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class RegisterPanel extends JPanel implements ActionListener {
    private ATM frame;
    private JLabel welcome = new JLabel("欢迎注册中国银行账户");
    private JLabel accountLabel = new JLabel("账号：");
    private JLabel pwdLabel = new JLabel("密码：");
    private JLabel pwdConfirmLabel = new JLabel("确认密码：");
    private JTextField accountTextField = new JTextField();
    private JPasswordField pwdField = new JPasswordField();
    private JPasswordField pwdConfirmTextField = new JPasswordField();
    private JButton backBtn = new JButton(Util.getIcon("/images/buttonOff.jpg"));
    private JButton registerBtn = new JButton(Util.getIcon("/images/buttonLogin.jpg"));
    private Font font = new Font("宋体", Font.BOLD, 25);


    public RegisterPanel(ATM frame) {
        this.frame = frame;
        this.setLayout(null);
        this.setOpaque(false);

        //欢迎设置
        welcome.setBounds(350, 10, 400, 50);
        welcome.setFont(font);
        this.add(welcome);
        //账户提示设置
        accountLabel.setBounds(40, 400, 100, 50);
        accountLabel.setFont(font);
        this.add(accountLabel);
        //账户输入框设置
        accountTextField.setBounds(200, 400, 200, 50);
        accountTextField.setFont(font);
        this.add(accountTextField);

        pwdLabel.setBounds(40, 450, 100, 50);
        pwdLabel.setFont(font);
        this.add(pwdLabel);

        pwdField.setBounds(200, 450, 200, 50);
        pwdField.setFont(font);
        this.add(pwdField);

        pwdConfirmLabel.setBounds(40, 500, 200, 50);
        pwdConfirmLabel.setFont(font);
        this.add(pwdConfirmLabel);

        pwdConfirmTextField.setBounds(200, 500, 200, 50);
        pwdConfirmTextField.setFont(font);
        this.add(pwdConfirmTextField);

        registerBtn.setBounds(40, 550, 140, 50);
        registerBtn.addActionListener(this);
        this.add(registerBtn);

        backBtn.setBounds(240, 550, 140, 50);
        backBtn.addActionListener(this);
        this.add(backBtn);
    }


    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == registerBtn) {
            //获取用户表
            ArrayList<User> users = frame.getUsers();
            // 获取输入的账号
            String account = accountTextField.getText();
            for (int i = 0; i < users.size(); i++) {
                if (account.equals(users.get(i).getAccount())) {
                    JOptionPane.showMessageDialog(this, "账号已存在");
                    return;
                }
            }
            // 获取输入的密码，与确认密码
            String pwd = String.valueOf(pwdField.getPassword());
            String pwdConfirm = String.valueOf(pwdConfirmTextField.getPassword());
            // 判断两次输入的密码是否相同
            if (!pwd.equals(pwdConfirm)) {
                JOptionPane.showMessageDialog(this, "两次密码不一致");
                return;
            }
            //更新到用户表及其数据库
            User newUser = new User(account, pwd, 0);
            users.add(newUser);
            MysqlUtil.insert(newUser);
            JOptionPane.showMessageDialog(this, "注册成功");
            //返回登录界面
            frame.switchPanel("login");

        } else if (e.getSource() == backBtn) {//返回登录界面
            frame.switchPanel("login");
        }
    }
}
